package yf.mall.service.impl;

import yf.mall.dao.AccountDao;
import yf.mall.dao.UserDao;
import yf.mall.entity.AccountEntity;
import yf.mall.entity.UserEntity;
import com.yf.service.impl.BaseServiceImpl;
import yf.mall.enums.AccountTypeEnum;
import yf.mall.enums.LevelTypeEnum;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import yf.mall.dao.LevelDao;
import yf.mall.entity.LevelEntity;
import yf.mall.service.LevelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 级别Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Jul 11 12:00:51 CST 2019
 */
@Service
public class LevelServiceImpl extends BaseServiceImpl<LevelEntity> implements LevelService {

    private LevelDao levelDao;

    @Autowired
    private AccountDao accountDao;


    @Autowired
    public void setDao(LevelDao levelDao) {
        this.levelDao = levelDao;
        super.setBaseDao(levelDao);
    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/

    @Override
    public List<LevelEntity> findLowLevel(Long userId) {
        List<LevelEntity> levelEntities = levelDao.queryList(null);
        UserEntity userEntity = userDao.get(userId);
        LevelEntity levelEntity = levelDao.get(userEntity.getLevelId());
        levelEntities = levelEntities.stream().filter(level ->
                level.getLv() > levelEntity.getLv() && level.getType().equals(LevelTypeEnum.PUB.getIndex())
        ).collect(Collectors.toList());
        return levelEntities;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LevelEntity changeLevel(Long userId, Long toUserId, Long levelId) {
        UserEntity userEntity = userDao.get(userId);
        UserEntity toUser = userDao.get(toUserId);
        if (!toUser.getPid().equals(userEntity.getId())) {
            throw new RRException("无权限操作!");
        }
        //不可以操作内部级别用户
        if (levelDao.get(toUser.getLevelId()).getType().equals(LevelTypeEnum.PRI.getIndex())) {
            throw new RRException("无权限对此级别用户操作!");
        }
        LevelEntity toLevel = levelDao.get(levelId);
        if (!toLevel.getType().equals(LevelTypeEnum.PUB.getIndex())) {
            throw new RRException("非法操作级别");
        }
        LevelEntity level = levelDao.get(userEntity.getLevelId());
        if (level.getLv() >= toLevel.getLv()) {
            throw new RRException("非法操作级别!");
        }
        toUser.setLevelId(levelId);
        userDao.update(toUser);
        return toLevel;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<LevelEntity> findlist() {
        List<LevelEntity> levelList = this.levelDao.findLevelList();
        return levelList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LevelEntity changeLevelByExp(Long userId, BigDecimal exp) {
        //比较当前人的经验值跟自己的级别是否同步 如果比自己的级别该有的少 则补上
        LevelEntity levelEntity = levelDao.findByUserId(userId);
        LevelEntity temLv = findByExp(exp);
        UserEntity userEntity = userDao.get(userId);
        if (temLv.getType()==LevelTypeEnum.PUB.getIndex()) {
            userEntity.setLevelId(temLv.getId());
        }
        userDao.update(userEntity);
        return levelEntity;
    }

    @Override
    public LevelEntity findByUserId(Long userId) {
        return levelDao.findByUserId(userId);
    }

    @Override
    public LevelEntity findByExp(BigDecimal exp) {
        LevelEntity levelEntity = levelDao.findBase();
        List<LevelEntity> levelEntities = levelDao.findLevelList();
        for (LevelEntity level : levelEntities) {
            if (exp.compareTo(level.getAmount()) >= 0) {
                levelEntity = level;
            }
        }
        return levelEntity;
    }

    @Autowired
    private UserDao userDao;
}
